package com.pengtu.web.project.org;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.Organization;
import com.pengtu.entity.project.Qualification;
import com.pengtu.service.app.SystemManager;
import com.pengtu.service.project.SignOrganizationManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;
/**
 * 单位资质Action
 * 
 * QualificationAction
 * 
 * 2011-7-27 下午02:44:47的
 * 
 * @version 1.0.0
 *
 */
@Results({
		@Result(name = BaseActionSupport.RELOAD, type = "redirect", location = "qualification.action?organizationId=${organizationId}&projectId=${projectId}&typeId=${typeId}&deptId=${deptId}")
		})
public class QualificationAction extends BaseActionSupport<Qualification> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private SignOrganizationManager signOrgManager;

	private String id;
	private Qualification qualification;
	private String organizationId;
	private Attachment attachmention;
	private SystemManager systemManager;
	private Organization organizations;
	private String projectId;
	private String typeId;
	private Page<Qualification> page = new Page<Qualification>(10);
	private String deptId;
	private String signid;
	public String getSignid() {
		return signid;
	}

	public void setSignid(String signid) {
		this.signid = signid;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryfilters = QueryFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		queryfilters.addFilter(new String[] { "organization.id" },
				organizationId);
		if (!page.isOrderBySetted()) {
			page.orderBy("createDate");
			page.order(Page.ASC);
		}
		page = signOrgManager.findPageQualification(page, queryfilters);
		// System.out.println(page.getResult());
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(organizationId) && StringUtils.isEmpty(id))
			organizations = signOrgManager.getOrg("id", organizationId);
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(organizationId)) {
			Organization org = new Organization();
			org.setId(organizationId);
			if (null != attachmention)
				qualification.setQualificationFile(attachmention
						.getUpfileFileName());
			qualification.setOrganization(org);
		}
		signOrgManager.saveQualification(qualification);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes+"单位资质");
		if (null != attachmention) {
			attachmention.setTableId(qualification.getId());
			attachmention.setTableField("qualificationFile");
			attachmention.setTableName("Qualification");
			systemManager.uploadFile(Struts2Utils.getAppRootPath(),
					attachmention);
		}
		return "successMessage";
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		signOrgManager.deleteQualification(id);
		addActionMessage("删除单位资质");
		return RELOAD;
	}

	public void checkCode() {
		// TODO Auto-generated method stub
		String code = Struts2Utils.getParameter("code");
		String oldCode = Struts2Utils.getParameter("oldCode");
		if (signOrgManager.isQuaCodeUnique(code, oldCode)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}
	
	
	public void deletQua(){
		signOrgManager.deleteQualification(id);
		addActionMessage("删除单位资质");
		Struts2Utils.renderText("1");
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			qualification = signOrgManager.getQualification("id", id);
		} else {
			qualification = new Qualification();
		}
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	public Page<Qualification> getPage() {
		return page;
	}

	public Organization getOrganizations() {
		return organizations;
	}

	public Qualification getQualification() {
		return qualification;
	}

	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}

	public Attachment getAttachmention() {
		return attachmention;
	}

	public void setAttachmention(Attachment attachmention) {
		this.attachmention = attachmention;
	}

	public String getOrganizationId() {
		return organizationId;
	}

	public void setOrganizationId(String organizationId) {
		this.organizationId = organizationId;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setSignOrgManager(SignOrganizationManager signOrgManager) {
		this.signOrgManager = signOrgManager;
	}

	@Override
	public Qualification getModel() {
		// TODO Auto-generated method stub
		return qualification;
	}

}
